package com.lz.ionicbase.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.lz.ionicbase.R;
import com.lz.ionicbase.ui.activity.PsdInputCodeActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/2/18.
 */

public class Mycode extends LinearLayout implements TextWatcher, View.OnKeyListener {
    //验证码的位数
    private int codeNumber;
    //两个验证码之间的距离
    private int codeSpace;
    //验证码边框的边长
    private int lengthSide;
    //验证码的大小
    private float mtextSize;
    //字体颜色
    private int textColor = Color.BLACK;
    private int inputType = InputType.TYPE_CLASS_TEXT;
    private LayoutParams mEditparams;
    private LayoutParams mViewparams;

    private List<EditText> mEditTextList = new ArrayList<EditText>();
    private int currentPosition = 0;
    private Context mContext;
    private PsdInputCodeActivity codeview;
    private int codecount=0;

    public Mycode(Context context) {
        super(context);
    }

    public Mycode(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.psd_code);
        codeNumber = typedArray.getInteger(R.styleable.psd_code_codeNumber, 6);
        codeSpace = typedArray.getInteger(R.styleable.psd_code_codeSpace, 20);
        lengthSide = typedArray.getInteger(R.styleable.psd_code_lengthSide, 50);
        mtextSize = typedArray.getFloat(R.styleable.psd_code_textSize, 20);
//        inputType = typedArray.getInteger(R.styleable.psd_code_inputType, 2);
        mEditparams = new LayoutParams(lengthSide, lengthSide);
        mViewparams = new LayoutParams(codeSpace, lengthSide);
        initView();
    }

    private void initView() {
        for (int i = 0; i < codeNumber; i++) {
            EditText editText = new EditText(mContext);
            editText.setCursorVisible(false);
            editText.setOnKeyListener(this);
            editText.setTextSize(mtextSize);
            editText.setInputType(inputType);
            editText.setTextColor(textColor);
            editText.setPadding(0, 0, 0, 0);
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
            editText.addTextChangedListener(this);
            editText.setLayoutParams(mEditparams);
            editText.setGravity(Gravity.CENTER);
            editText.setBackgroundResource(R.drawable.psd_code__selector);
            addView(editText);
            mEditTextList.add(editText);
            if (i != codeNumber - 1) {
                View view = new View(mContext);
                view.setLayoutParams(mViewparams);
                addView(view);
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    //自动调到下一个格子
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.i("-------",count+"count");
        Log.i("-------",start+"start");
        Log.i("-------",before+"before");
        Log.i("-------",s+"");
        Log.i("-------","-------------");
        if (start == 0 && count >= 1 && currentPosition != mEditTextList.size() - 1) {
            currentPosition++;
            mEditTextList.get(currentPosition).requestFocus();
        }
        if (before==0) {
            codecount++;
            Log.i("-------",codecount+"codecount");
            codeview.getCodeCount(codecount);
        }else{
            codecount--;
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }


    /**
     * 监听删除键
     * @param view
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {
        EditText editText = (EditText) view;
        if (keyCode == KeyEvent.KEYCODE_DEL && editText.getText().length() == 0) {
            int action = event.getAction();
            if (currentPosition != 0 && action == KeyEvent.ACTION_DOWN) {
                currentPosition--;
                mEditTextList.get(currentPosition).requestFocus();
                mEditTextList.get(currentPosition).setText("");
            }
        }
        return false;
    }
    /**
     * 获取验证码
     * @return
     */
    public String getVerificationCode() {
        StringBuffer stringBuffer = new StringBuffer();
        for (EditText string : mEditTextList) {
            stringBuffer.append(string.getText().toString());
        }
        return stringBuffer.toString();
    }
    /**
     * 删除验证码
     */
    public void clearCode() {
        for (EditText string : mEditTextList) {
            String str = string.getText().toString();
            if (str!=null){
                string.setText("");
            }
        }
        if (currentPosition==mEditTextList.size()-1){
            currentPosition=0;
            mEditTextList.get(currentPosition).requestFocus();
        }
    }
    public void getCodeView(PsdInputCodeActivity codeview){
        this.codeview=codeview;
    }

}
